//
//  PinDanViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/16.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "PinDanViewController.h"
#import "PinDanData.h"
#import "PinDanCell.h"
#import "MyPinDanViewController.h"
#import "DetailPinDanViewController.h"
@interface PinDanViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView *_image;
    UILabel *_txtlbl;
}
@property(nonatomic,assign)NSInteger pageIndex;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation PinDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.dataSource=[NSMutableArray array];
    self.title=@"拼单";
    _pageIndex=1;
    [self setUpBgView];
    [self getPinDanData];
    [self setUpTableView];
    [self setUptopView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
#pragma mark--我的拼单视图
-(void)setUptopView
{
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(10, 69, 20, 20)];
    image.image=[UIImage imageNamed:@"wenhao"];
    [self.view addSubview:image];
    
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(image.frame.origin.x+20+5, 65, 80, 30)];
    [btn setTitle:@"了解净净拼单" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(aboutJingJingPinDan) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *myPinDan=[[UIButton alloc]initWithFrame:CGRectMake(kWidth-10-80, 69, 80, 30)];
    [myPinDan setTitle:@"我的拼单" forState:UIControlStateNormal];
    myPinDan.backgroundColor=BLUECOLOR;
    myPinDan.layer.cornerRadius=5;
    myPinDan.clipsToBounds=YES;
    [myPinDan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    myPinDan.titleLabel.font=[UIFont systemFontOfSize:15];
    [myPinDan addTarget:self action:@selector(MyPinDan) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myPinDan];
    
}
//什么是净净拼单
-(void)aboutJingJingPinDan
{
    WebViewController *webView=[[WebViewController alloc]init];
    webView.pageUrl=ABOUT_JingJingPinDan;
    webView.title=@"净净拼单";
    [self.navigationController pushViewController:webView animated:YES];
}
//我的拼单
-(void)MyPinDan
{
    MyPinDanViewController *myPinDan=[[MyPinDanViewController alloc]init];
    myPinDan.title=@"我的拼单";
    myPinDan.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:myPinDan animated:YES];
}
#pragma mark--没有拼单活动图
-(void)setUpBgView
{
    _image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"no_spell_order"]];
    _image.center=self.view.center;
    _image.hidden=YES;
    _image.bounds=CGRectMake(0, 5, 80, 80);
    [self.view addSubview:_image];
    
    _txtlbl=[[UILabel alloc]initWithFrame:CGRectMake(_image.frame.origin.x-_image.frame.size.width/2.0, _image.frame.origin.y+80+20, 200, 30)];
    _txtlbl.text=@"目前还没有拼单活动";
    _txtlbl.textColor=GRAYCOLOR;
    _txtlbl.hidden=YES;
    [self.view addSubview:_txtlbl];
}
#pragma mark-- 表
-(void)setUpTableView
{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64 ) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=240;
    _tableView.hidden=YES;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addTheMoreData)];
    [_tableView registerNib:[UINib nibWithNibName:@"PinDanCell" bundle:nil] forCellReuseIdentifier:@"pinDan"];
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PinDanCell *cell=[_tableView dequeueReusableCellWithIdentifier:@"pinDan" forIndexPath:indexPath];
    PinDanData *data=self.dataSource[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:data.ListImgUrl] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.title.text=data.SpellName;
    cell.remark.text=data.SpellIntro;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)addTheMoreData
{
    _pageIndex++;
    [self getPinDanData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PinDanData *data=self.dataSource[indexPath.row];
    DetailPinDanViewController *detailPinDan=[[DetailPinDanViewController alloc]init];
    detailPinDan.spellID=data.SpellBaseID;
    detailPinDan.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:detailPinDan animated:YES];
}
#pragma mark--获取拼单数据
-(void)getPinDanData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //http://192.168.1.107:3722/Activity/SpellHome/{UserCode}/{CityCode}/{ClientType}/{PageIndex}/{PageSize}/{Timestamp}/{Sign}
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSString *pageIndex=[NSString stringWithFormat:@"%ld",(long)_pageIndex];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"CityCode":GETCITYCODE,
                        @"ClientType":CLIENTTYPE,
                        @"PageIndex":pageIndex,
                        @"PageSize":@"10",
                        @"Timestamp":timeStamp,
                        };
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Activity/SpellHome/%@/%@/%@/%@/%@/%@/%@",GETUSERCODE,GETCITYCODE,CLIENTTYPE,pageIndex,@"10",timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"请求失败，请检查网络"];
        }
        if ([responses[@"IsSuccess"]intValue]==1) {
            if ([responses[@"ReturnObject"] count]==0) {
                //没有数据
                _image.hidden=NO;
                _txtlbl.hidden=NO;
                _tableView.hidden=YES;
            }else
            {
                _image.hidden=YES;
                _txtlbl.hidden=YES;
                _tableView.hidden=NO;
                NSArray *arr=responses[@"ReturnObject"];
                for (NSDictionary *dic in arr) {
                    PinDanData *data=[[PinDanData alloc]initWithDic:dic];
                    [self.dataSource addObject:data];
                }
                [_tableView reloadData];
                [_tableView.mj_header endRefreshing];
                if(_dataSource.count < 10)
                {
                    [_tableView.mj_footer endRefreshingWithNoMoreData];
                }else
                {
                    [_tableView.mj_footer endRefreshing];
                }

            }
        }else
        {
          
        }
        [MBProgressHUD hideHUDForView:self.view];
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
